import { GetListByPage } from '@/axios/api/ProjectAPI'

const project = {
    namespaced: true,
    state: () => ({  
        tabledata: [],
        showmodal: false,
        title: '',
        creatrouter: '',
        editrouter: '',
        fullscreen: true,
        tablecolumns: [],
        currentProjectID: '',
        total: 0,
        pageIndex: 1,
        pageSize: 15,
        pageSizeOpts: [15,25,50,100]
    }),
    mutations: { 
        UPDATE_TABLE_COLUMNS: (state,payload) => {
            //console.log(payload)
            payload = [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    type: 'index',
                    title: '序号',
                    minWidth: 80,
                    align: 'center'
                },
                {
                    title: '项目编码',
                    key: 'NumCode',
                    minWidth: 200,
                    align:'center',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.NumCode)
                    }
                },
                {
                    title: '所属公司',
                    key: 'Company',
                    minWidth: 185,
                    align:'center',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.Company)
                    }
                },
                {
                    title: '项目名称',
                    key: 'Name',
                    minWidth: 350,
                    align:'center',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.Name)
                    }
                },
                {
                    title: '工程类型',
                    key: 'ItemType',
                    align:'center',
                    minWidth: 100,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.ItemType)
                    }
                },
                {
                    title: '项目财务',
                    key: 'Accountant',
                    align:'center',
                    minWidth: 100,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.Accountant)
                    }
                },
                {
                    title: '项目余额',
                    key: 'EndBalance',
                    align:'center',
                    minWidth: 150,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.EndBalance)
                    }
                },
                {
                    title: '冻结资金',
                    key: 'FreezeMoney',
                    align:'center',
                    minWidth: 150,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.FreezeMoney)
                    }
                },
                {
                    title: '可用余额',
                    align: 'center',
                    children: [
                        {
                            title: '合计',
                            key: 'FlowBalance',
                            align:'center',
                            minWidth: 150,
                            render:(h,params)=>{
                                return h('div',{style: { textAlign:"left" }},params.row.FlowBalance)
                            }
                        },
                        {
                            title: '现金',
                            key: 'CashBalance',
                            align:'center',
                            minWidth: 150,
                            render:(h,params)=>{
                                return h('div',{style: { textAlign:"left" }},params.row.CashBalance)
                            }
                        },
                        {
                            title: '商票',
                            key: 'CommercialTicket',
                            align:'center',
                            minWidth: 150,
                            render:(h,params)=>{
                                return h('div',{style: { textAlign:"left" }},params.row.CommercialTicket)
                            }
                        },
                        {
                            title: '工抵房',
                            key: 'GongDiFang',
                            align:'center',
                            minWidth: 150,
                            render:(h,params)=>{
                                return h('div',{style: { textAlign:"left" }},params.row.GongDiFang)
                            }
                        }
                    ]
                }
            ]
            state.tablecolumns = payload;
        },
        UPDATE_TABLE_DATA: (state,payload) => {
            //console.log(payload)
            /* payload = [
                {
                    Guid: '17gd-djgk-gdkgj-gdkf12-dfg',
                    ProjectName: '交行诸暨支行',
                    ProjectAccount: '1211024409059906581',
                    AccountTail: '6581',
                    AccountType: '一般户',
                    BeginBalance: 0.00,
                    EndingBalance: 0.00,
                    FreezeMoney: 0.00,
                    FlowBalance: 0.00
                }
            ]; */
            state.total = payload.totalCount;
            state.tabledata = payload.items;
        },
        CHANGE_TITLE: (state,payload) => {
            state.title = payload;
        },
        CHANGE_MODAL: (state) => {
            state.showmodal = !state.showmodal;
        },
        SETTING_ID: (state,payload) => {
            state.currentProjectID= payload;
        },
        SETTING_PAGEINDEX: (state,payload) =>{
            state.pageIndex = payload;
        },
        SETTING_PAGESIZE: (state,payload) =>{
            state.pageSize = payload;
        },
        SETTING_CREATROUTER: (state,payload) => {
            state.creatrouter = payload;
        },
        SETTING_EDITROUTER: (state,payload) => {
            state.editrouter = payload;
        },
        CREATE: (state,payload) => {
            state.tabledata.push(payload)
        },
        UPDATE:(state,payload) => {
            let index = state.tabledata.findIndex(item => item.AccountGuid === payload.Guid);
            state.tabledata.splice(index,1,payload)
        }
    },
    actions: {
        InitProject({commit},payload){
            commit('SETTING_CREATROUTER',payload.CreatRouter)
            commit('SETTING_EDITROUTER',payload.EditRouter)
            //console.log(payload)
            
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });

            
            //commit('UPDATE_TABLE_DATA',payload.TableData);
            commit('UPDATE_TABLE_COLUMNS',payload.TableColumns);
        },
        ChangePageIndex({commit},payload){
            commit('SETTING_PAGEINDEX',payload.PageIndex);
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });
        },
        ChangePageSize({commit},payload){
            commit('SETTING_PAGESIZE',payload.PageSize);
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });
        },
        UpdateTableAsync({commit},payload){
            /* setTimeout(() => {
                
            }, 1000) */
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });
        },
        CreateView({commit}){
            commit('CHANGE_TITLE',"创建项目信息")
            commit('CHANGE_MODAL');
            commit('SETTING_ID','');
        },
        UpdateView({commit},payload){
            commit('CHANGE_TITLE',"编辑项目信息")
            commit('CHANGE_MODAL');
            commit('SETTING_ID',payload);
        },
        Cancel({commit}){
            commit('CHANGE_MODAL');
        }
    },
    getters: { 
        tablecolumns: state => { state.tablecolumns },
        tabledata: state => state.tabledata,
        showmodal: state => state.showmodal,
        pageIndex: state => { state.pageIndex },
        pageSize: state => state.pageSize
        /* creatrouter: state => state.creatrouter,
        editrouter: state => state.editrouter */
    }
}

export default project